package com.example.administrator.yunniuusedcar.fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.common.base.Global;
import com.example.administrator.yunniuusedcar.R;
import com.example.administrator.yunniuusedcar.adapter.MainNewInfoAdapter;
import com.example.administrator.yunniuusedcar.adapter.newsZiXunAdapter;
import com.example.administrator.yunniuusedcar.model.mode_benn.IHttpService;
import com.example.administrator.yunniuusedcar.model.mode_benn.WZ_ArticlenListBean;
import com.example.administrator.yunniuusedcar.model.mode_benn.ZD_ArticleTopBean;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;

import okhttp3.Call;

/**
 * 资讯 当日头条
 * Created by Administrator on 2017/12/6.
 */

public class FragmentNews02 extends Fragment {

    private RecyclerView recyclerView3;    //Fragment
    private MainNewInfoAdapter infoAdapter;
    private ArrayList listDatas;
    private newsZiXunAdapter ziXunAdapter;
    private ListView listView;

//    ArrayList<WZ_ArticleInfoBean.Data> listDatas = new ArrayList();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        TextView textView = new TextView(getContext());
        textView.setText(getContext().getClass().getSimpleName());  // 获取名字
        textView.setTextColor(Color.RED);
        textView.setTextSize(18);
        textView.setGravity(Gravity.CENTER);
/////
        View view = inflater.inflate(R.layout.item_recycleview2, null);           //fragment_choosecar02_informationi
        recyclerView3 = view.findViewById(R.id.recycler_view6);
//
        recyclerView3.setLayoutManager(new LinearLayoutManager(getContext()));
        //创建数据
        listDatas = new ArrayList();
        initUloadMenuInfor();    //// 置顶文章( 获取数据 )
        initUloadListInfor();    // 文章列表

/////
        return view;
    }

    private void initUloadMenuInfor() {    // 加载数据

        String ul = IHttpService.HOST_URL + "Article/getTop";    //置顶文章

        OkHttpUtils.post().url(ul).addParams("sign", IHttpService.SignVal).build().execute(new StringCallback() {     //.addParams("type","1")   //改

            @Override
            public void onError(Call call, Exception e, int id) {
                Global.showToast("请求超时！" + e.getMessage());
                System.out.println("-----====----------A" + e);
            }

            @Override
            public void onResponse(String response, int id) {
                //        Global.showToast("====="+response);
                System.out.println("-----====-----------A1" + response);
                Gson js = new Gson();
                ZD_ArticleTopBean wzArticleInfoBean = js.fromJson(response, ZD_ArticleTopBean.class);
                listDatas.add(wzArticleInfoBean);
//                Info02ActionAdapter info02ActionAdapter = new Info02ActionAdapter(getContext(),listDatas);
                ziXunAdapter = new newsZiXunAdapter(getContext(), listDatas);  //改
                recyclerView3.setAdapter(ziXunAdapter);
                //   infoAdapter.setdata(listDatas);
            }
        });
    }

    private void initUloadListInfor() {    // 加载数据 文章 列表

        String ul = IHttpService.HOST_URL + "Article/getList";    //文章 列表  //Article/getList

        OkHttpUtils.post().url(ul).addParams("sign", IHttpService.SignVal).addParams("class_id", "9").build()
                .execute(new StringCallback() {

                    private WZ_ArticlenListBean wzArticleInfoBean;

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Global.showToast("请求超时！" + e.getMessage());
                        System.out.println("-----====----------A" + e);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Global.showToast("=====" + response);
                        System.out.println("-----====-----------A1" + response);
                        Gson js = new Gson();
                        wzArticleInfoBean = js.fromJson(response, WZ_ArticlenListBean.class);
                        listDatas.add(wzArticleInfoBean);
//              Info02ActionAdapter info02ActionAdapter = new Info02ActionAdapter(getContext(),listDatas);
                        ziXunAdapter = new newsZiXunAdapter(getContext(), listDatas);
                        recyclerView3.setAdapter(ziXunAdapter);
                        //   infoAdapter.setdata(listDatas);

                    }
                });
    }
}
